package top.eggcode.system.modules.user.control;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import top.eggcode.common.base.PackBaseController;
import top.eggcode.common.lang.BeanUnit;
import top.eggcode.common.model.ListPage;
import top.eggcode.common.model.ResponseResult;
import top.eggcode.common.mvc.ParamValidator;
import top.eggcode.common.mvc.Responser;
import top.eggcode.plugins.dict.Translate;
import top.eggcode.system.modules.user.model.*;
import top.eggcode.system.modules.user.service.AccountRoleServiceImpl;
import top.eggcode.system.modules.user.service.UserServiceImpl;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * Title: 用户信息
 * Description: TODO
 * Date: 2021/4/22 22:31
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Api(tags = "用户信息")
@RequestMapping("/api/user")
@RestController
public class UserController extends PackBaseController {

    @Resource
    private UserServiceImpl selfService;

    @Resource
    private UserServiceImpl userService;

    @Resource
    private AccountRoleServiceImpl relationService;

    @GetMapping("/{id}")
    public ResponseResult<?> get(@PathVariable String id) {
        return super.pack(userService.getFull(id).get(), UserView::build);
    }

    @Translate
    @GetMapping("/list/page")
    public ResponseResult<?> listPage(UserQuery query) {
        ListPage<UserBiz> page = userService.page(query);
        List<UserView> viewList = new LinkedList<>();
        page.getList()
                .stream()
                .map(UserView::build)
                .forEach(viewList::add);
        ListPage<UserView> pageView = new ListPage<>();
        pageView.setNumber(page.getNumber())
                .setSize(page.getSize())
                .setTotal(page.getTotal())
                .setList(viewList);

        return Responser.successful(pageView);
    }

    @DeleteMapping("/{id}/delete")
    public ResponseResult<?> delete(@PathVariable String id) {
        selfService.deleteById(id);
        return Responser.successful();
    }

    @PatchMapping("/update")
    public ResponseResult<?> update(@RequestBody UserForm form) {
        ParamValidator.required(form, "username", "password", "nickname");

        selfService.update(BeanUnit.transfer(form, UserEntity.class));

        // 设置角色
        AccountEntity account = userService.findByUserId(form.getId()).get();
        relationService.clearRoles(account.getId());
        List<String> roleIdList = form.getRoleIdList();
        roleIdList.forEach(roleId -> {
            AccountRoleEntity relation = new AccountRoleEntity();
            relation.setAccountId(account.getId());
            relation.setRoleId(roleId);
            relation.init();
            relationService.add(relation);
        });
        return Responser.successful();
    }

    @PostMapping("/add")
    public ResponseResult<?> add(@RequestBody UserForm form) {

        ParamValidator.required(form, "username", "password", "nickname");

        userService.save(form.convert());

        return Responser.successful();
    }

    @GetMapping("/init")
    public ResponseResult<?> initUser() {
        return super.pack(selfService.init().get());
    }
}
